package cn.edu.mju.property.service.impl;

import cn.edu.mju.property.dao.UserDao;
import cn.edu.mju.property.dao.impl.UserDaoImpl;
import cn.edu.mju.property.entity.PageBean;
import cn.edu.mju.property.entity.User;
import cn.edu.mju.property.service.UserService;
import com.alibaba.fastjson.JSONArray;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();

    @Override
    public List<User> findAll() {
        return dao.findAll();
    }

    @Override
    public boolean addUser(User user) {
       return dao.add(user);
    }

    @Override
    public boolean delSelected(JSONArray ids) {
       for (Object id: ids) {
           dao.delSelected(id.toString());
        }
        return true;
    }

    @Override
    public User findUser(String id) {
        return dao.findUser(id);
    }

    @Override
    public boolean updateUser(User user) {
        return dao.updateUser(user);
    }

    @Override
    public PageBean<User> findUserByPage(String _currentPage, String _rows) {

        int currentPage = Integer.parseInt(_currentPage) ;
        int rows = Integer.parseInt(_rows) ;

        PageBean<User> pb = new PageBean<User>();
        pb.setCurrentPage(currentPage);
        pb.setRows(rows);

        //总记录数
        int totalCount = dao.findTotalCount();
        pb.setTotalCount(totalCount);

        //开始值
        int start = (currentPage - 1) * rows;
        List<User> list = dao.findByPage(start,rows);
        pb.setList(list);

        int totalPage = totalCount % rows == 0 ? (totalCount/rows) : (totalCount/rows) + 1;
        pb.setTotalPage(totalPage);

        return pb;
    }
}
